package com.dios.mirage.component.view;

import java.io.DataInputStream;
import java.io.OutputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.io.file.FileConnection;

import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Keypad;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.PopupScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;

public class ListView extends HorizontalFieldManager {

	private int imageIndex = 0;

	public ListView() {
		super(Manager.HORIZONTAL_SCROLL);
	}

	public void addImage(String image) {
		imageIndex++;
		add(new ImageField(image));
	}

	private class ImageField extends BitmapField {

		VerticalFieldManager popupItem = new VerticalFieldManager();

		public ImageField(String image) {
			super(View.createImage(image, Display.getWidth(),
					new Double(Display.getWidth() * (30 / 100)).intValue())
					.getBitmap(), Field.FOCUSABLE);
			init();
		}

		public void init() {
			popupItem
					.add(new LabelField("Download Wallpaper", Field.FOCUSABLE) {

						protected boolean navigationClick(int status, int time) {
							final int index = 73 - imageIndex;
							String url = "http://www.ktb.co.id/mirage/gallery/wallpaper/"
									.concat(String.valueOf(index));
							UiApplication.getUiApplication().invokeLater(new HTTPConnect(url));
							return super.navigationClick(status, time);
						}
					});
		}

		protected boolean navigationClick(int status, int time) {
			synchronized (UiApplication.getEventLock()) {
				UiApplication.getUiApplication().pushScreen(
						new PopupScreen(popupItem) {
							protected boolean keyDown(int keycode, int time) {
								if (Keypad.KEY_ESCAPE == Keypad.key(keycode)) {
									UiApplication.getUiApplication().popScreen(
											this);
								}
								return super.keyDown(keycode, time);
							}
						});
			}
			return super.navigationClick(status, time);
		}

	}

	public class HTTPConnect implements Runnable {
		HttpConnection _httpConnection;
		String url;

		public HTTPConnect(String url) {
			this.url = url.concat(";deviceside=true");
		}

		public void run() {
			try {
				FileConnection file;
				_httpConnection = (HttpConnection) Connector.open(url,
						Connector.READ);
				_httpConnection.setRequestMethod(HttpConnection.GET);

				DataInputStream _dis = _httpConnection.openDataInputStream();

				/*int status = _httpConnection.getResponseCode();
				if (status == HttpConnection.HTTP_OK) {
				}*/

				//int _c;
				file = (FileConnection) Connector
						.open("file:///Device/home/user/documents/a.jpg", Connector.READ_WRITE);
				if (!file.exists()) {
					file.create();
	                Dialog.alert("Created file");
				}
				OutputStream out = file.openOutputStream();
                /*byte[] readBlock = new byte[256];

				while ((_c = _dis.read(readBlock)) != -1) {
					out.write(readBlock, 0, _c);
				}*/

				_dis.close();
                _httpConnection.close();
                _dis = null;
                _httpConnection = null;
                out.close();
                file.close();
                Dialog.alert("Saved to disk");
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
	}
}
